package com.ruoyi.web.controller.system;

import com.ruoyi.system.domain.SysHome;
import com.ruoyi.system.service.SysHomeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.SysDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页信息维护Controller
 * 
 * @author xx
 * @date 2020-04-16
 */
@Controller
@RequestMapping("system/home")
public class SysHomeController extends BaseController {
    private String prefix = "system/home";

    @Autowired
    private SysHomeService sysHomeService;
    @Autowired
    private SysDictDataService sysDictDataService;

    @RequiresPermissions("system:home:view")
    @GetMapping()
    public String home() {
        return prefix + "/home";
    }

    /**
     * 查询首页信息维护列表
     */
    @RequiresPermissions("system:home:list")
    @PostMapping("list")
    @ResponseBody
    public TableDataInfo list(SysHome sysHome) {
        startPage();
        List<SysHome> list = sysHomeService.selectSysHomeList(sysHome);
        return getDataTable(list);
    }

    /**
     * 导出首页信息维护列表
     */
    @RequiresPermissions("system:home:export")
    @Log(title = "首页信息维护", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    @ResponseBody
    public AjaxResult export(SysHome sysHome) {
        List<SysHome> list = sysHomeService.selectSysHomeList(sysHome);
        ExcelUtil<SysHome> util = new ExcelUtil<>(SysHome.class);
        return util.exportExcel(list, "home", sysDictDataService.selectAll(SysHome.class));
    }

    /**
     * 新增首页信息维护
     */
    @GetMapping("add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存首页信息维护
     */
    @RequiresPermissions("system:home:add")
    @Log(title = "首页信息维护", businessType = BusinessType.INSERT)
    @PostMapping("add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysHome sysHome) {
        return toAjax(sysHomeService.insertSysHome(sysHome));
    }

    /**
     * 修改首页信息维护
     */
    @GetMapping("edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysHome sysHome = sysHomeService.selectSysHomeById(id);
        mmap.put("sysHome", sysHome);
        return prefix + "/edit";
    }

    /**
     * 修改保存首页信息维护
     */
    @RequiresPermissions("system:home:edit")
    @Log(title = "首页信息维护", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysHome sysHome) {
        return toAjax(sysHomeService.updateSysHome(sysHome));
    }

    /**
     * 删除首页信息维护
     */
    @RequiresPermissions("system:home:remove")
    @Log(title = "首页信息维护", businessType = BusinessType.DELETE)
    @PostMapping("remove")
    @ResponseBody
    public AjaxResult remove(SysHome sysHome) {
        return toAjax(sysHomeService.deleteSysHomeByIds(sysHome));
    }

    @RequiresPermissions("system:home:open")
    @Log(title = "首页信息维护-修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysHome sysHome) {
        return toAjax(sysHomeService.changeStatus(sysHome));
    }
}
